

require "mjexenum"

local filelog = require "filelog"
local math = math
local pairs = pairs
local ipairs = ipairs
local type = type
local table = table

--胡牌算法
local MJ = require "algo_utils"

local cardTool = { }

--------------------

--[Comment]
--机器人出牌策略
--
local function robot_method_c(cards,all_outcards,all_dicards,que_color)
    local tmp = {}
    for _,card in pairs(cards) do
        if card < 100 then
            local ct,cv = math.floor(card / 10),card % 10
            tmp[ct] = tmp[ct] or {}
            tmp[ct][cv] = (tmp[ct][cv] or 0) + 1
       end
    end

    --优先出缺牌
    if tmp[que_color] then
        for card,cnt in pairs(tmp[que_color]) do
            return (que_color * 10 + card)
        end
    end

    --获得所有打出的牌
    local out_cards = { }
    for i = 1,31 do
        out_cards[i] = 4
    end
    out_cards[10] = 0
    out_cards[20] = 0
    out_cards[30] = 0
    out_cards[31] = 0

    for _,vs in pairs(all_outcards) do
        for _,card in pairs(vs) do
            if card < 100 then
                out_cards[card] = out_cards[card] - 1
            end
        end
    end
    for _,vs in pairs(all_dicards) do
        for _,item in pairs(vs) do
            if item.card < 100 then
                if item.gang_peng_type == 5 then
                    out_cards[item.card] = out_cards[item.card] - 3
                elseif item.card > 0 then
                    out_cards[item.card] = out_cards[item.card] - 4
                end
            end
        end
    end

    local may_cards = { }
    for _,card in pairs(cards) do
        if card < 100 then
            out_cards[card] = out_cards[card] - 1
            may_cards[card] = 0
        end
    end
    
    --判断每一张牌
    for ct,vs in pairs(tmp) do
        for card,cnt in pairs(vs) do
            --1、
            --可以构成杠子+16
            --可以构成刻子+4
            --可以构成顺子+3
            --可以构成对子+2

            --2、
            -- - min(构成杠子代价,构成顺子代价,构成刻子代价,构成对子代价)
            local realcard = ct * 10 + card
            if cnt == 4 then
                may_cards[realcard] = may_cards[realcard] + 16
            elseif cnt == 3 then
                may_cards[realcard] = may_cards[realcard] + 4
            elseif cnt == 2 then
                may_cards[realcard] = may_cards[realcard] + 2
            end

            --判断是否能构成顺子
            if (vs[card+1] and vs[card+2]) then
                may_cards[realcard] = may_cards[realcard] + 3
            end
            if (vs[card-1] and vs[card+1]) then
                may_cards[realcard] = may_cards[realcard] + 3
            end
            if (vs[card-1] and vs[card-2]) then
                may_cards[realcard] = may_cards[realcard] + 3
            end 

            --杠子、刻子、顺子、对子都不能构成,判断构成顺子的代价
            if may_cards[realcard] == 0 then
                local sunzi_cost = -100

                --顺子代价
                if card < 8 and vs[card+1] then
                    sunzi_cost = sunzi_cost + out_cards[realcard + 2]
                end
                if card < 8 and vs[card+2] then
                    sunzi_cost = sunzi_cost + out_cards[realcard + 1]
                end
                if card > 2 and vs[card-1] then
                    sunzi_cost = sunzi_cost + out_cards[realcard - 2]
                end
                if card > 2 and vs[card-2] then
                    sunzi_cost = sunzi_cost + out_cards[realcard - 1]
                end
                if card ~= 1 and card ~= 9 then
                    if vs[card-1] then sunzi_cost = sunzi_cost + out_cards[realcard + 1] end
                    if vs[card+1] then sunzi_cost = sunzi_cost + out_cards[realcard - 1] end
                end 
                may_cards[realcard] = sunzi_cost
            end
        end
    end
    
    local min = 100
    local result = cards[#cards]
    for card,score in pairs(may_cards) do
        if min > score then
            min = score
            result = card
        end
    end

    if may_cards[result] == nil then
        result = cards[#cards]
    end
    
    return result
end

--[Comment]
--机器人出牌策略2
--
local function robot_method_b(cards,all_outcards,all_dicards,que_color,othercards)
    local tmp = {}
    for _,card in pairs(cards) do
        if card < 100 then
            local ct,cv = math.floor(card / 10),card % 10
            tmp[ct] = tmp[ct] or {}
            tmp[ct][cv] = (tmp[ct][cv] or 0) + 1
       end
    end

    --优先出缺牌
    if tmp[que_color] then
        for card,cnt in pairs(tmp[que_color]) do
            return (que_color * 10 + card)
        end
    end

    --获得所有打出的牌
    local out_cards = { }
    for i = 1,31 do
        out_cards[i] = 4
    end
    out_cards[10] = 0
    out_cards[20] = 0
    out_cards[30] = 0
    out_cards[31] = 0

    for _,vs in pairs(all_outcards) do
        for _,card in pairs(vs) do
            if card < 100 then
                out_cards[card] = out_cards[card] - 1
            end
        end
    end
    for _,vs in pairs(all_dicards) do
        for _,item in pairs(vs) do
            if item.card < 100 then
                if item.gang_peng_type == 5 then
                    out_cards[item.card] = out_cards[item.card] - 3
                elseif item.card > 0 then
                    out_cards[item.card] = out_cards[item.card] - 4
                end
            end
        end
    end

    if othercards then
        for _,vs in pairs(othercards) do
            for _,card in pairs(vs) do
                if card < 100 then
                    out_cards[card] = out_cards[card] - 1
                end
            end
        end
    end

    local may_cards = { }
    for _,card in pairs(cards) do
        if card < 100 then
            out_cards[card] = out_cards[card] - 1
            may_cards[card] = 0
        end
    end

    --剃掉顺子
    for ct,vs in pairs(tmp) do
        for card = 1,9 do
            if vs[card] then
                --判断是否能构成顺子
                if (vs[card+1] and vs[card+2]) then
                    vs[card] = (vs[card] == 1 and nil) or (vs[card] - 1)
                    vs[card+1] = (vs[card+1] == 1 and nil) or (vs[card+1] - 1)
                    vs[card+2] = (vs[card+2] == 1 and nil) or (vs[card+2] - 1)
                end
                if (vs[card] and vs[card-1] and vs[card+1]) then
                    vs[card] = (vs[card] == 1 and nil) or (vs[card] - 1)
                    vs[card-1] = (vs[card-1] == 1 and nil) or (vs[card-1] - 1)
                    vs[card+1] = (vs[card+1] == 1 and nil) or (vs[card+1] - 1)
                end
                if (vs[card] and vs[card-1] and vs[card-2]) then
                    vs[card] = (vs[card] == 1 and nil) or (vs[card] - 1)
                    vs[card-1] = (vs[card-1] == 1 and nil) or (vs[card-1] - 1)
                    vs[card-2] = (vs[card-2] == 1 and nil) or (vs[card-2] - 1)
                end
            end
        end
    end
    
    --判断每一张牌
    for ct,vs in pairs(tmp) do
        for card,cnt in pairs(vs) do
            --1、
            --可以构成杠子+16
            --可以构成刻子+4
            --可以构成顺子+3
            --可以构成对子+2

            --2、
            -- - min(构成杠子代价,构成顺子代价,构成刻子代价,构成对子代价)
            local realcard = ct * 10 + card
            if cnt == 4 then
                may_cards[realcard] = may_cards[realcard] + 16
            elseif cnt == 3 then
                may_cards[realcard] = may_cards[realcard] + 4
            elseif cnt == 2 then
                if not vs[card-1] and not vs[card+1] then
                    may_cards[realcard] = may_cards[realcard] + 2
                end
            end

            --判断是否能构成顺子
            if (vs[card+1] and vs[card+2]) then
                may_cards[realcard] = may_cards[realcard] + 3
            end
            if (vs[card-1] and vs[card+1]) then
                may_cards[realcard] = may_cards[realcard] + 3
            end
            if (vs[card-1] and vs[card-2]) then
                may_cards[realcard] = may_cards[realcard] + 3
            end 

            --杠子、刻子、顺子、对子都不能构成,判断构成顺子的代价
            if may_cards[realcard] == 0 then
                local sunzi_cost = -100

                --顺子代价
                if card < 8 and vs[card+1] then --顺子左边一张
                    sunzi_cost = sunzi_cost + out_cards[realcard + 2]
                end
                if card < 8 and vs[card+2] then --顺子左边一张
                    sunzi_cost = sunzi_cost + out_cards[realcard + 1]
                end
                if card > 1 and vs[card+1] then --顺子中间一张
                    sunzi_cost = sunzi_cost + out_cards[realcard - 1]
                end
                if card < 9 and vs[card-1] then --顺子中间一张
                    sunzi_cost = sunzi_cost + out_cards[realcard + 1]
                end
                if card > 2 and vs[card-1] then --顺子右边一张
                    sunzi_cost = sunzi_cost + out_cards[realcard - 2]
                end
                if card > 2 and vs[card-2] then --顺子右边一张
                    sunzi_cost = sunzi_cost + out_cards[realcard - 1]
                end
                may_cards[realcard] = sunzi_cost
            end
        end
    end
    
    local min = 100
    local results = {cards[#cards]}
    for card,score in pairs(may_cards) do
        if min > score then
            min = score
            results = {card}
        elseif min == score then
            table.insert(results,card)
        end
    end

    local result = nil
    if #results > 0 then
        for _,card in pairs(results) do
            if card % 10 == 1 or card % 10 == 9 then
                result = card
                break
            end
        end
        if not result then
            local index = math.random(1,#results)
            result = results[index]
        end
    end

    result = result or 0
    if may_cards[result] == nil then
        result = cards[#cards]
    end
    
    return result
end

----------------------------------------------------------------------
--外部接口

--[Comment]
-- 获得随机牌堆
-- ret 牌堆数组，赖子值为：花色*100 + 牌值（红中为841）
--
function cardTool.rand_card_list(player_num)
    return MJ.shuffle_cards(player_num)
end

--[Comment]
-- 发所有玩家牌
-- ret 牌数组，第一个子数组为庄家牌组{[1],[2],[3],[4]}
--
function cardTool.deal_cards(cards_stack,dice_one,dice_two,player_num,is_7zhang)
    local allcards = MJ.deal_cards(cards_stack,dice_one,dice_two,player_num,is_7zhang)

    --测试
	--[[
    if is_7zhang then
        allcards[1] = {11,2,2,2,6,6,8,8}       
        allcards[2] = {11,11,11,12,12,12,2,}
    else
        allcards[1] = {1,1,1,1,2,2,2,2,3,3,3,3,9,9}       
        allcards[2] = {11,11,12,12,13,13,14,14,15,15,16,16,17}
    end
    if player_num >= 3 then
        allcards[3] = {21,22,23,24,25,26,27,28,29,14,14,15,16} 
    end
    if player_num >= 4 then
        allcards[4] = {17,17,17,18,18,18,19,19,19,15,15,15,16}
    end

    cards_stack.cards = cardTool.rand_card_list(player_num)
    cards_stack.init_num = #(cards_stack.cards)
    for i = 1,player_num do
        cardTool.delete_cards(cards_stack.cards,allcards[i])
    end

    local first = 1
    local last = #(cards_stack.cards)
    for i = first + 1,last do
        local k = first + math.random(0,i - first)
        local tmp = cards_stack.cards[k]
        cards_stack.cards[k] = cards_stack.cards[i]
        cards_stack.cards[i] = tmp
    end

    cards_stack.cur_num = last
    cards_stack.prePos = 0
	]]
    --测试
    cardTool.print_cards_stack(cards_stack)

    return allcards
end

--[Comment]
-- 获得推荐换三张牌组
-- ret 换三张牌数组
--
function cardTool.get_exchange_cards(cards)
    return MJ.get_suggestExchangeCards(cards)
end

--[Comment]
-- 获得推荐缺牌花色
-- ret 推荐缺牌花色 0筒 1条 2万
--
function cardTool.get_suggest_que(cards)
    return MJ.get_suggestQue(cards)
end

--[Comment]
-- @desc 检查玩家是否胡牌
-- @hand_cards玩家手牌
-- @touch_bar_cards 玩家碰杠牌
-- @cur_card 其他玩家出的牌,为nil表示为自己摸牌
-- @is_banker 是否为庄家
-- @round_num 回合数，初始为0
-- @que_color 缺牌花色
-- @ret isHuPai,fanshu  胡牌true，不胡牌false,胡牌时返回番数组
--
function cardTool.check_hu(hand_cards,touch_bar_cards,cur_card,is_banker,round_num,que_color,is_7zhang)
    hand_cards = hand_cards or { }
    touch_bar_cards = touch_bar_cards or { }
    local t = { handCards = hand_cards,touchBarCards = touch_bar_cards}
    local is_mopai = false
    if cur_card == nil then
        is_mopai = true
    end
    local is_7dui = true
    if is_7zhang then
        is_7dui = false
    end

    return MJ.check_isHuPai(t,cur_card,is_mopai,is_banker,round_num,que_color,true,nil,is_7zhang)
end

--[Comment]
-- 判断是否听牌
-- hand_cards 手牌
-- is_ret_ting_cards 是否返回听牌数组
-- ret 听牌返回true，听牌数组 失败返回false
--
function cardTool.check_is_tingpai(hand_cards,is_ret_ting_cards,que_color,is_7zhang)
    return MJ.is_tingPai(hand_cards,is_ret_ting_cards,que_color,true)
end

--[Comment]
-- 
--  算番
--
function cardTool.calculate_fan(hand_cards,di_cards,hupai_types,table_conf,hu_card)
    local di_values = { }
    local is_zimo = false
    local cards = MJ.deepcopy(hand_cards)

    if hupai_types[EMJHuPaiType.HUPAI_TYPE_ZIMO] then
        is_zimo = true
    end
    if not is_zimo then
        table.insert(cards,hu_card)
    end

    --获得碰杠牌
    for _,item in pairs(di_cards) do
        table.insert(di_values,item.card)
        table.insert(di_values,item.card)
        table.insert(di_values,item.card)
        if item.gang_peng_type ~= EMJGangPengType.GANGPENG_TYPE_PENG then
            table.insert(di_values,item.card)
        end
    end

    local t_cards = MJ.count_sort(cards)
    local t_otherCards = MJ.count_sort(di_values)
    
    --新开放基本番

    if table_conf.is_dai_yaojiu then    --带幺九，幺九2番 清幺九4番
        MJ.is_qingYaoJiu(t_cards,t_otherCards,hupai_types)
    end

    if table_conf.is_jiangdui then      --将对,3番
        MJ.is_jiangDui(t_cards,t_otherCards,hupai_types)
    end

    if table_conf.is_tiandi_hu then     --天地胡，5番
        if hupai_types[EMJHuPaiType.HUPAI_TYPE_DIHU] ~= nil then
            if hupai_types.max_type < hupai_types[EMJHuPaiType.HUPAI_TYPE_DIHU] then
                hupai_types.max_type = EMJHuPaiType.HUPAI_TYPE_DIHU
            end
        end

        if hupai_types[EMJHuPaiType.HUPAI_TYPE_TIANHU] ~= nil then
            if hupai_types.max_type < hupai_types[EMJHuPaiType.HUPAI_TYPE_TIANHU] then
                hupai_types.max_type = EMJHuPaiType.HUPAI_TYPE_TIANHU
            end
        end
    else
        hupai_types[EMJHuPaiType.HUPAI_TYPE_DIHU] = nil
        hupai_types[EMJHuPaiType.HUPAI_TYPE_TIANHU] = nil
    end

    filelog.sys_info("max hu pai type: " .. hupai_types.max_type)

    local total_fan = 0
    local gou_num = cardTool.get_gou_num(cards,di_cards,hupai_types)
    local fan,gou = MJ.calculate_fanShu(cards,gou_num,hupai_types)
    filelog.sys_info("gou shu: " .. gou,"base bei shu: " .. fan)

    --额外加番情况

    if hupai_types[EMJHuPaiType.HUPAI_TYPE_GANG_SHANG_HUA] then  --杠上花，加1番
        fan = fan * 2
    end

    if hupai_types[EMJHuPaiType.HUPAI_TYPE_GANG_SHANG_PAO] then  --杠上炮，加1番
        fan = fan * 2
    end

    if hupai_types[EMJHuPaiType.HUPAI_TYPE_GANG_QIANG_GANG] then --抢杠，加1番
        fan = fan * 2
    end

    if table_conf.is_menqing then    
        if  MJ.is_menQing(di_cards,EMJGangPengType.GANGPENG_TYPE_ANGANG,is_zimo) then    --门清,1番
            hupai_types[EMJHuPaiType.HUPAI_TYPE_MENQING] = true
            fan = fan * 2
        end
    end

    if table_conf.is_zhongzhang then 
        if MJ.is_zhongZhang(cards,hu_card,is_zimo) then       --中张,1番
             hupai_types[EMJHuPaiType.HUPAI_TYPE_ZHONGZHANG] = true
             fan = fan * 2
        end
    end

    if table_conf.is_duan_yaojiu then    
        if MJ.is_duanYaoJiu(t_cards,t_otherCards) then          --断幺九,1番
            hupai_types[EMJHuPaiType.HUPAI_TYPE_DUAN_YAOJIU] = true
            fan = fan * 2
        end
    end

    if table_conf.is_haidilao then
        if hupai_types[EMJHuPaiType.HUPAI_TYPE_HAIDILAO] then   --海底捞，1番
            fan = fan * 2
        end
    elseif hupai_types[EMJHuPaiType.HUPAI_TYPE_HAIDILAO] then
        hupai_types[EMJHuPaiType.HUPAI_TYPE_HAIDILAO] = nil
    end

   if table_conf.is_haidipao then
       if hupai_types[EMJHuPaiType.HUPAI_TYPE_HAIDIPAO] then    --海底炮，1番
            fan = fan * 2
       end
   elseif hupai_types[EMJHuPaiType.HUPAI_TYPE_HAIDIPAO] then
        hupai_types[EMJHuPaiType.HUPAI_TYPE_HAIDIPAO] = nil
   end

    if is_zimo then
        if table_conf.zimo_add_multi_mode == EMJZiMoAddMultiMode.ADD_BASE then          --自摸加底
            fan = fan + 1
        elseif table_conf.zimo_add_multi_mode == EMJZiMoAddMultiMode.ADD_MULTI then     --自摸加倍
            fan = fan * 2
        end
    end

    total_fan = fan
    filelog.sys_info("final bei shu: " .. fan)

   --最大为封顶番数
    if fan > table_conf.max_multi_num then
        fan = table_conf.max_multi_num
    end

    filelog.sys_info("final real bei shu: " .. fan)

    return fan,total_fan
end

--[Comment]
--
-- 打印牌堆
--
function cardTool.print_cards_stack(cards_stack)
    local str = ""
    for i,v in pairs(cards_stack.cards) do
        str = str .. i .. "--" .. v .. "\n"
    end

    filelog.sys_info(str)
end

--[Comment]
-- 摸一张牌
-- ret 返回摸的牌
--
function cardTool.touch_card(cards_stack,rid)
    local card = MJ.touch_oneCard(cards_stack)
    if  card ~= nil then
        filelog.sys_info(rid ,"touch card ok, cur_num  " .. cards_stack.cur_num,"card " .. card)
    else
        filelog.sys_error(rid,"touch card failed, cur_num  " .. cards_stack.cur_num)
    end
    return card
end

--[Comment]
-- 获得杠牌数组
-- ret 返回杠牌数组，没有杠牌时数组为{}
--
function cardTool.get_gang_cards(cards,que_color)
    return MJ.get_gangCards(cards,que_color)
end

--[Comment]
-- 判断是否能碰
--
function cardTool.check_peng(cards,card,que_color)
    if que_color == math.floor(card/10) then
        return false
    end
    return MJ.check_isCanPeng(cards,card,que_color)
end

--[Comment]
-- 判断是否能杠
--
function cardTool.check_gang(hand_cards,touch_bar_cards,card,is_mopai)
    if cardTool.check_peng_gang(hand_cards,touch_bar_cards,card,is_mopai) ~= false then
        return true
    end
    return false
end

--[Comment]
-- 判断是否能碰杠
-- hand_cards 手牌
-- touch_bar_cards 碰杠牌
-- ret 1明杠 2暗杠 3添杠 false不能杠
--
function cardTool.check_peng_gang(hand_cards,touch_bar_cards,card,is_mopai)
    local t = { handCards = hand_cards,touchBarCards = touch_bar_cards}
    return MJ.check_isCanGang(t,card,is_mopai)
end

--[Comment]
-- 删除一张牌（用于出牌）
-- ret 成功返回true，反之false
--
function cardTool.delete_card(src_cards,card,rid)
    if MJ.find_andDelOneCard(src_cards,card) then
        filelog.sys_info(rid,"delete card ok,card " .. card)
        return true
    else
        filelog.sys_error(rid,"delete card falied,card ".. card)
    end

    return false
end

--[Comment]
-- 删除一组牌
-- ret 返回删除数量
--
function cardTool.delete_cards(src_cards,cards,rid)
    local num = 0
    for index,card in pairs(cards) do
        if cardTool.delete_card(src_cards,card,rid) then
            num = num + 1
        end
    end
    
    return num
end

--[Comment]
-- 检查一张牌
-- ret 成功，返回true，失败返回false
--
function cardTool.check_card(cards,dest_card, que_color)
    if not dest_card or dest_card <= 0 then
        return false
    end

    if not MJ.find_oneCard(cards,dest_card) then
        return false
    end

    local cur_color = math.floor(dest_card / 10)
    local tmp = {}
    for _,card in pairs(cards) do
        if card < 100 then
            local ct = math.floor(card / 10)
            if ct == que_color and cur_color ~= que_color then
                return false
            end
       end
    end

    return true
end

--[Comment]
-- 查找一张牌
-- ret 成功，返回true，失败返回false
--
function cardTool.find_card(cards,dest_card)
    return MJ.find_oneCard(cards,dest_card)
end

--[Comment]
-- 查找一组牌
--
function cardTool.find_cards(cards,dest_cards)
    for i = 1,#dest_cards do
        if not cardTool.findOneCard(cards,dest_cards[i]) then
            return false
        end
    end

    return true
end

--[Comment]
--插入一张牌
--
function cardTool.insert_card(src_cards,newcard,rid)
    return MJ.insert_oneCard(src_cards,newcard)
end

--[Comment]
--插入一组牌
--
function cardTool.insert_cards(src_cards,cards,rid)
      for i=1,#cards do
        MJ.insert_oneCard(src_cards,cards[i])
    end
end

function cardTool.print_cards(cards)
    return MJ.print_cards(cards)
end

--[Comment]
-- --检查玩家剔除杠牌以后是否有叫
--
function cardTool.is_gang_youjiao(cards,gang_card,que_color)
    local tmp = MJ.deepcopy(cards)
    for i = 1,4 do
        cardTool.delete_card(tmp,gang_card)
    end

    return MJ.is_tingPai(tmp,false,que_color)
end

--[Comment]
-- --检查玩家是否为花猪
--
function cardTool.is_hua_zhu(cards)
    local t_cards = MJ.count_sort(cards)
    return MJ.is_huaZhu(t_cards)
end

--[Comment]
--获得机器人出牌
--
function cardTool.get_robot_chupai(cards,all_outcards,all_dicards,que_color)
    return robot_method_b(cards,all_outcards,all_dicards,que_color)
end

--[Comment]
--获得托管出牌
--
function cardTool.get_tuoguan_chupai(cards,que_color)
    --优先出缺牌
    local len = 0
    for _,card in pairs(cards) do
        if card < 100 then
            local ct = math.floor(card / 10)
            if ct == que_color then
                return card
            end
       end
       len = len + 1
    end

    --没有缺牌，出最后一张
    return cards[len]
end

--[Comment]
--判断碰牌是否划算
--
function cardTool.check_is_good_peng(cards,cur_card,que_color)
    if math.floor(cur_card / 10) == que_color then
        return false
    end

    local tmp = {}
    local cardtype,cardval = math.floor(cur_card / 10),cur_card % 10
    for _,card in pairs(cards) do
        if card < 100 then
            local ct,cv = math.floor(card / 10),card % 10
            if cardtype == ct then
                tmp[cv] = (tmp[cv] or 0) + 1
            end
       end
    end

    if not tmp[cardval-1] and not tmp[cardval+1] then
        return true
    end

   if not tmp[cardval-1] and tmp[cardval+2] then
        return false
   end

   if not tmp[cardval+1] and tmp[cardval-2] then
        return false
   end

   return true
end

--[Comment]
--获得勾数
--
function cardTool.get_gou_num(hand_cards, di_cards, fan_types)
    local tmp = {}
    for k,v in pairs(hand_cards) do
        tmp[v] = (tmp[v] or 0) + 1
    end
    
    local gou_num = 0
    for _,cnt in pairs(tmp) do
        if cnt == 4 then
            gou_num = gou_num + 1
        end
    end

    for _,item in pairs(di_cards) do
        if item.gang_peng_type == EMJGangPengType.GANGPENG_TYPE_PENG then
            if tmp[item.card] == 1 then
                gou_num = gou_num + 1
            end
        else
            gou_num = gou_num + 1
        end
    end

    if fan_types.max_type == EMJHuPaiType.HUPAI_TYPE_LONGQIDUI
    or fan_types.max_type == EMJHuPaiType.HUPAI_TYPE_QING_LONGQIDUI then
        gou_num = gou_num - 1
    end
    
    return gou_num
end

return cardTool
----------------------------------------------------------


